package com.cxh.androidmedia.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cxh.androidmedia.utils.CCLog;
import com.cxh.androidmedia.utils.DimenUtil;

public class FaceView extends View {

    private float[] mFaces = null;
    private Paint mPaint = new Paint();

    public FaceView(Context context) {
        this(context, null);
    }

    public FaceView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint.setStrokeWidth(DimenUtil.dp2Px(context, 5));
        mPaint.setColor(Color.RED);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);

        if (null == mFaces || mFaces.length < 2) {
            return;
        }

        for (int i = 0; i < mFaces.length; i += 2) {
            if (i + 1 < mFaces.length) {
                float x = mFaces[i] * getWidth();
                float y = mFaces[i + 1] * getHeight();

                canvas.drawPoint(x, y, mPaint);
            }
        }
    }

    public void setFaces(float[] faces) {
        if (null != faces) {
            CCLog.e("face: " + faces.length);
        }

        mFaces = faces;
        invalidate();
    }

}
